//
//  ViewController.m
//  Sunexpress
//
//  Created by 筱亮 on 2017/5/8.
//  Copyright © 2017年 曹亮. All rights reserved.
//

#import "ViewController.h"
#import "HomepageViewController.h"
#import "drawerView.h"
#import <Masonry.h>
#import "MyaccountViewController.h"
@interface ViewController ()
{
    BOOL buttag;
}
@property(nonatomic,strong)drawerView * dView;
@property(nonatomic,strong)HomepageViewController *complainListVC;
@property(nonatomic,strong)UIView * MyaddView;

@end
#define MyWframe [UIScreen mainScreen].bounds.size.width
#define MyHframe [UIScreen mainScreen].bounds.size.height
@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.MyaddView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.MyaddView];
    
    UIButton * mtbut = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    [mtbut setImage:[UIImage imageNamed:@"user"] forState:0];
    [mtbut addTarget:self action:@selector(Clickbut:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * butitme =  [[UIBarButtonItem alloc]initWithCustomView:mtbut];
    self.navigationItem.leftBarButtonItem = butitme;

    // 1.添加所有子控制器
    [self setupChildViewController];
    
    [self setupSegmentedControl];
    if (!_dView) {
        self.dView = [[drawerView alloc]initWithFrame:CGRectMake(0, 0, MyWframe-150, MyHframe)];
        
        self.dView.backgroundColor = [UIColor whiteColor];
        [self.dView.addressBut addTarget:self action:@selector(address) forControlEvents:UIControlEventTouchUpInside];
        [self.dView.orderBut addTarget:self action:@selector(order) forControlEvents:UIControlEventTouchUpInside];
        [self.dView.Headportrait addTarget:self action:@selector(Headportrait) forControlEvents:UIControlEventTouchUpInside];
        [self.view insertSubview:self.dView atIndex:0];
        self.dView.alpha = 0;
    }

}
-(void)viewWillAppear:(BOOL)animated{
        buttag = NO;
}
//头像按钮
-(void)Headportrait{
    
    [self.navigationController pushViewController:[MyaccountViewController new] animated:NO];
    
}
//订单
-(void)order{
    
}
//地址
-(void)address{
    
}
-(void)Clickbut:(UIButton *)mybut{
    if (buttag == NO) {
        buttag = YES;
        [UIView animateWithDuration:0.5 animations:^{
            self.navigationController.navigationBar.frame = CGRectMake(MyWframe-150, 0, MyWframe, 64);
            self.MyaddView.frame = CGRectMake(MyWframe-150, 0, MyWframe, MyHframe);
            self.dView.alpha = 1;
            
        } completion:^(BOOL finished){
            
        }];
    }
    else{
        buttag = NO;
        [UIView animateWithDuration:0.5 animations:^{
            self.navigationController.navigationBar.frame = CGRectMake(0, 0, MyWframe, 64);
            self.MyaddView.frame = CGRectMake(0, 0, MyWframe, MyHframe);
            self.dView.alpha = 0;
            
        } completion:^(BOOL finished){
            
        }];
    }
}
- (void)setupSegmentedControl {
    
    NSArray  *title_arr = @[@"全部", @"早餐店", @"餐车", @"优惠"];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * title_arr.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = YES;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = YES;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.MyaddView addSubview:_mainScrollView];
    
    
    self.SG = [SGSegmentedControl segmentedControlWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44) delegate:self segmentedControlType:(SGSegmentedControlTypeStatic) titleArr:title_arr];
    _SG.titleColorStateSelected = [UIColor redColor];
    _SG.indicatorColor = [UIColor orangeColor];
    [self.MyaddView addSubview:_SG];
    
    UIView * linView = [[UIView alloc]init];
    linView.frame = CGRectMake(0, 43+64, [UIScreen mainScreen].bounds.size.width, 1);
    linView.backgroundColor = [UIColor whiteColor];//底线
    [self.SG addSubview:linView];
    
    self.complainListVC = [[HomepageViewController alloc] init];
    [self.mainScrollView addSubview:self.complainListVC.view];
    [self addChildViewController:self.complainListVC];
    
    
    
}


- (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index {
    // 1 计算滚动的位置
    CGFloat offsetX = index * self.view.frame.size.width;
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 2.给对应位置添加对应子控制器
    [self showVc:index];
}

// 添加所有子控制器
- (void)setupChildViewController {
    NSArray  *title_arr = @[@"全部", @"早餐店", @"餐车", @"优惠"];
    for (int i = 0; i<title_arr.count; i++) {
        HomepageViewController *oneVC = [[HomepageViewController alloc] init];
        oneVC.tagString = i;
        [self addChildViewController:oneVC];
    }
}
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 1.添加子控制器view
    [self showVc:index];
    
    // 2.把对应的标题选中
    [self.SG titleBtnSelectedWithScrollView:scrollView];
}

-(void)viewDidDisappear:(BOOL)animated{

        self.navigationController.navigationBar.frame = CGRectMake(0, 0, MyWframe, 64);
        self.MyaddView.frame = CGRectMake(0, 0, MyWframe, MyHframe);
        self.dView.alpha = 0;
        
   
}


@end
